select first_name, length(first_name)
from customer;
select first_name, last_name, substring(first_name, 1,1) || substring(last_name, 1,1)
from customer;
select rental_id, date_part('hour', rental_date) as "rental hour"
from rental
where date_part('hour', rental_date) >= 22;
select payment_id, payment_date
from payment
order by payment_date desc
limit 3;
select rating,
case rating
when 'G' then 'General'
when
else 'Unknown'
end
from film;
select count(*),
count(email)
from customer;
select
count(*) filter(where return_date - rental_date < interval '3 days') as lt_3_days,
count(*) filter(where return_date - rental_date >= interval '3 days') as gte_3_days,
count(*) filter(where return_date is null) as never_returned
from rental;
select
case
when length <= 60 then '0 - 1hrs'
when length > 60 and length <= 120 then '1 - 2hrs'
when length > 120 and length <= 180 then '2 - 3hrs'
when length > 180 then '3hrs+'
else '0 - 1hrs'
end as length,
count(*)
from film
group by 1
order by length;
select
1.0 * sum(length) / count(*) as avg1,
1.0 * avg(length) as avg2
from film;
select customer_id, avg(return_date - rental_date) as avg_rent_duration
from rental
group by 1
order by 2 desc;
select customer_id
from payment
group by customer_id
having bool_and(amount > 2);
select rating, repeat('*', (count(*) / 10)::int) as "count/10"
from film
where rating is not null
group by rating;
select title || ' is ' || coalesce(length::text || ' minutes', 'unkown')
from film;
select left(title, 3) || repeat('*', length(title)-3)
from film;
select
round(100.0 * count(case when rating = 'NC-17' then 1 end)) / count(*) as NC_17,
round(100.0 * count(case when rating = 'G' then 1 end)) / count(*) as G,
round(100.0 * count(case when rating = 'PG' then 1 end)) / count(*) as PG,
round(100.0 * count(case when rating = 'PG-13' then 1 end)) / count(*) as PG_13,
round(100.0 * count(case when rating = 'R' then 1 end)) / count(*) as R
from film;
select int '33';
select int '33.3';
select cast(33.3 as int);
select cast(33.8 as int);
select 33::text;
select 'hello'::varchar(2);
select cast(35000 as smallint);
select 12.1::numeric(1,1);
select *
from pg_timezone_names;
select *
from pg_timezone_abbrevs;
select title,
make_interval(0, 0, 0, rental_duration, 0,0, 0.0) as duration,
make_interval(0, 0, 0, rental_duration, 0,0, 0.0) + '1 day'::interval as "duration + 1"
from film;
select date_part('hour',rental_date) as hr, count(*) as count
from rental
group by date_part('hour',rental_date)
order by date_part('hour',rental_date) asc;
select
date_trunc('month', payment_date) as "month",
sum(amount) as total
from payment
group by "month"
order by "month";
select count(*)
from rental
where date_trunc('day', rental_date) = date_trunc('month',rental_date) + interval '1 month' - interval '1 day';
select title
from film
where length(title) != length(trim(title));
select * from rental;
select customer_id, cast(date_part('epoch',sum(return_date - rental_date)) / 3600 as int)
from rental r
group by customer_id
order by 2 desc
limit 3;
select * from generate_series('2019-01-01 17:00 UTC'::timestamptz, '2019-12-31'::timestamp, '1 month'::interval);
select distinct first_name, char_length(first_name) - char_length(replace(first_name, 'A', ''))
from customer
order by 2 desc;
select sum(amount)
from payment
where extract(dow from payment_date) in (0, 6);
select c.first_name || ' ' || c.last_name,
count(r.rental_id)
from rental r
join customer c on c.customer_id = r.customer_id
where r.rental_date::date > to_date('2005-05-24', 'YYYY-MM-DD')
group by c.customer_id
order by count(r.rental_id) desc
;
with rental_rank as (
select customer_id,
rental_date
rental_id,
rank() over (partition by customer_id order by rental_date)
from rental)
select *
from rental_rank
where rank <= 3;
select rating
from (
select rating,
title,
rank() over (partition by rating order by title) as rank
from film) t
where rank = 1 and rating is not null;